/** \brief File implementing a simple grid made of diamonds for the game of
 * life.
 *
 * This file contains the implementation of a class implementing a simple grid
 * for the game of life, where the cells are diamonds.
 *
 * @file DiamondGrid.cpp
 * @author Johan "Solo" Luisier
 * @date 2013/08/24
 */
#include "DiamondGrid.hpp"

#include "Cell.hpp"

#include <QtCore/QDebug>

const quint32 DiamondGrid::MaxNeighbourNumber( 8u );

DiamondGrid::DiamondGrid( const quint32 &w, const quint32 &h, QObject *parent )
    : GridBase( w, h, parent )
{}

DiamondGrid::~DiamondGrid()
{}

bool DiamondGrid::initialiseNeighbours()
{
    quint32 i, j;

    /**
     * The complicated pattern for border Cell instances is somehow delegated
     * to the GridBase::operator(), which returns zero for unvalid indices.
     */
    for ( i = 0u; i < height(); i++ )
    {
        for ( j = 0u; j < width(); j++ )
        {
            operator()( i, j ) -> addNeighbour( operator()( i, j - 2u ) );
            operator()( i, j ) -> addNeighbour( operator()( i, j + 2u ) );
            operator()( i, j ) -> addNeighbour( operator()( i - 1u, j ) );
            operator()( i, j ) -> addNeighbour( operator()( i + 1u, j ) );

            operator()( i, j ) -> addNeighbour( operator()( i, j - 1u ) );
            operator()( i, j ) -> addNeighbour( operator()( i + 1u, j - 1u ) );
            operator()( i, j ) -> addNeighbour( operator()( i, j + 1u ) );
            operator()( i, j ) -> addNeighbour( operator()( i + 1u, j + 1u ) );
        }
    }

    for ( i = 0u; i < height(); i++ )
    {
        for ( j = 0u; j < width(); j++ )
        {
            operator()( i, j ) -> setLifeRules( GameRules );
        }
    }

    qDebug() << tr( "DiamondGrid::initialiseNeighbours specific initialisation done" );

    CellNumber = width() * height();

    return true;
}
